<?php

/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 16/4/8
 * Time: 下午1:35
 */
class ChannelService extends Service{

    public static function indexHeader(){
        $header = array(
                'id'             => array('text' => '序号', 'width' => '80px'),
                'channel_name'   => array('text' => '渠道名称', 'width' => '160px'),
        		'tab'            => array('text' => '渠道ID', 'width' => '150px'),
                'special_name'   => array('text' => '所属专服', 'width' => '160px'),
        		'game_id'        => array('text' => '专服ID', 'width' => '100px'),
        		'version'        => array('text' => '版本号', 'width' => '120px'),
        		'app_address'    => array('text' => 'app更新地址', 'width' => '180px'),
        		'recharge_key'   => array('text' => '充值key', 'width' => '150px'),
        		'is_force'       => array('text' => '强制更新', 'width' => '100px'),
        		'is_open'        => array('text' => '充值开启', 'width' => '100px'),
        		'ios_verify'     => array('text' => 'ios审核', 'width' => '100px'),
        		'create_time'   => array('text' => '创建时间', 'width' => '150px'),
                'action'        => array('text' => '操作', 'width' => '60px'),
        );

        return $header;
    }
    
    public static function indexData($params){
    	$modChannel = new ModChannel();
        $result = $modChannel->page($params);
        
//         $modSpecialChannel = new ModSpecialChannel();
//         $spList = $modSpecialChannel->getSpecialChannel();
//         $specialList = array();
//         foreach ($spList as $key=>$val){
//         	$specialList[$val['channel_id']]['name'] = $val['name'];
//         	$specialList[$val['channel_id']]['special_id'] = $val['special_id'];
//         }
    
    	$length = count($result['data']);
    	foreach($result['data'] as $k=>&$v){
    		$type = 1;
    		if($k == 0 && $length == 1){  //当前页只剩下一条数据时，删除之后需显示上一页的数据
    			$type = 2;
    		}
    		
    		if($v['is_force'] == 0){
    			$force = '否';
    		}
    		if($v['is_force'] == 1){
    			$force = '是';
    		}
    		$v['is_force'] = $force;
    		 
    		if($v['is_open'] == 0){
    			$open = '否';
    		}
    		if($v['is_open'] == 1){
    			$open = '是';
    		}
    		$v['is_open'] = $open;
    		 
    		//$v['special_name'] = $specialList[$v['id']]['name'];
    		$v['special_name'] = self::getSpecial($v['id']);
    		$v['create_time']     = date('Y-m-d H:i:s', $v['create_time']);
    		//$v['action']        = '<a href="'.PubFunc::buildUrl('/oss/?r=channel/setServer').'&id='.$v['id'].'">服务器</a>';
    		$v['action']        .= ' <a href="'.PubFunc::buildBackUrl('/oss/?r=channel/edit').'&id='.$v['id'].'">编辑</a>';
    		$v['action']        .= ' <a href="'.PubFunc::buildBackUrl('/oss/?r=channel/delete',$type).'&id='.$v['id'].'" onclick="del()" confirm="是否要删除该条服信息?">删除</a>';
    	}
    	unset($v);
    
    	return $result;
    }
    
    public static function getSpecial($id){
    	$modSpecialChannel = new ModSpecialChannel();
    	$data = $modSpecialChannel->getSpecial($id);
    	$list = '';
    	foreach ($data as $key=>$val){
    		$list .= ','.$val['name'];
    	}
    	$list = substr($list, 1);
    	return $list;
    }


    public static function doAdd($params){
        $user = AccAuth::me();

        $modChannel = new ModChannel();
        $modChannel->game          = $params['g'];
        $modChannel->channel_num   = $params['tab'];
        $modChannel->channel_name  = $params['channel_name'];
        $modChannel->tab           = $params['tab'];
        $modChannel->create_user   = $user['UserID'];
        $modChannel->create_time   = time();
        $modChannel->update_user   = $user['UserID'];
        $modChannel->update_time   = time();
        
        $modChannel->game_id  = $params['game_id'];
        $modChannel->version  = $params['version'];
        $modChannel->app_address  = $params['app_address'];
        $modChannel->recharge_key  = $params['recharge_key'];
        $modChannel->is_force  = $params['is_force'];
        $modChannel->is_open  = $params['is_open'];
        $modChannel->ios_verify  = $params['ios_verify'];
        $result = $modChannel->save();
        
        if($result !== false){
         	$modSpecialChannel = new ModSpecialChannel();
//         	$modSpecialChannel->special_id = $params['special_id'];
//         	$modSpecialChannel->channel_id = $modChannel->id;
//         	$modSpecialChannel->save();        	
        	$special_array = $params['special_id'];
        	$special = array();
        	foreach ($special_array as $key=>$val){
        		$data = array();
        		$data['special_id'] = $val;
        		$data['channel_id'] = $modChannel->id;
        		array_push($special, $data);
        	}
        	$modSpecialChannel->addSpecialChannel($special);	
        }
        return $result;
    }

    public static function doEdit($id, $params){
        if(empty($id)) return false;

        $user = AccAuth::me();

        $modChannel = new ModChannel();
        $update = array();
        $update['game']         = $params['g'];
        $update['channel_num']   = $params['tab'];
        $update['channel_name']  = $params['channel_name'];
        $update['tab']       = $params['tab'];
        $update['update_user']  = $user['UserID'];
        $update['update_time']  = time();
        
        $update['game_id']  = $params['game_id'];
        $update['version']  = $params['version'];
        $update['app_address']  = $params['app_address'];
        $update['recharge_key']  = $params['recharge_key'];
        $update['is_force']  = $params['is_force'];
        $update['is_open']  = $params['is_open'];
        $update['ios_verify']  = $params['ios_verify'];

        $result = $modChannel->updateByPk($id, $update);
        if($result !== false){        	
        	//修改渠道对应的专服
        	//先删除
        	$modSpecialChannel = new ModSpecialChannel();
        	$modSpecialChannel->deleteAll('channel_id=:channel_id', array(':channel_id'=>$id));
        	//再添加
//         	$modSpecialChannel->special_id = $params['special_id'];
//         	$modSpecialChannel->channel_id = $id;
//         	$modSpecialChannel->save();
        	
        	$special_array = $params['special_id'];
        	$special = array();
        	$special_ids = '';
        	foreach ($special_array as $key=>$val){
        		$data = array();
        		$data['special_id'] = $val;
        		$data['channel_id'] = $id;
        		array_push($special, $data);
        		
        		$special_ids .= ','.$val;
        	}
        	$special_ids = substr($special_ids, 1);
        	$modSpecialChannel->addSpecialChannel($special);
        	
        	if($params['old_special_id'] != $special_ids){
        		//如果修改了专服，则需删除该渠道与服务器存在的关系，因为服务器的渠道必须是归属某个专服的，渠道修改了专服，服务器原来对应的专服就不存在了该渠道
        		$modServerChannel = new ModServerChannel();
        		$modServerChannel->deleteAll('channel_id=:channel_id', array(':channel_id'=>$id));
        	}

        }
        return true;
    }
    
    public static function repeat($id,$field,$value){
    	$modChannel = new ModChannel();
    	$conut = $modChannel::repeat($id,$field,$value);
    	if($conut == 0){
    		return false;
    	}else{
    		return true;
    	}
    }

    public static function get($id){
        $modChannel = new ModChannel();
        $obj = $modChannel->findByPk($id);

        if($obj){
            $info = $obj->getAttributes();

            return $info;
        }else{
            return false;
        }
    }

    public static function delete($id){
        if(empty($id)) return false;

        $modChannel = new ModChannel();
        $modChannel->deleteByPk($id);
        
        //删除渠道对应的专服
        $modSpecialChannel = new ModSpecialChannel();
        $modSpecialChannel->deleteAll('channel_id=:channel_id', array(':channel_id'=>$id));
        return true;
    }
    
    /**
     * 获取已经设置的服务器
     * @param unknown $id
     * @return multitype:unknown
     */
    public static function alreadyServer($id){
    	$modServerChannel = new ModServerChannel();
    	$params['channel_id'] = $id;
    	$list = $modServerChannel->getSelectServer($params);    	
    	$already = array();
    	foreach ($list as $key=>$val){
    		$already[] = $val['server_id'];
    	}
    	return $already;
    }
    
    /**
     * 根据服务器id渠道
     * @param unknown $id
     * @return multitype:unknown
     */
    public static function alreadyChannel($server_id){
    	$modServerChannel = new ModServerChannel();
    	$params['server_id'] = $server_id;
    	$list = $modServerChannel->select($params);
    	$already = array();
    	foreach ($list as $key=>$val){
    		$already[] = $val['channel_id'];
    	}
    	return $already;
    }
    
    /**
     * 开通服务器
     * @param unknown $params
     * @return boolean|unknown
     */
    public static function doChannel($params){
    	if(PubFunc::checkEmpty($params, array( 'channel_id', 'server_id'))){
    		return array('state' => false, 'msg' => '缺少必要参数.');
    	}
    	 
    	$modServerChannel = new ModServerChannel();
    	$server = array();
    	foreach ($params['server_id'] as $key=>$val){
    		$data = array();
    		$data['channel_id'] = $params['channel_id'];
    		$data['server_id'] = $val;
    		array_push($server, $data);
    	}
    	$result = $modServerChannel->addServerChannel($server);
    	return array('state' => $result, 'msg' =>'添加成功');
    }
    

    public static function batchDelete($channel_id){
    	$modServerChannel = new ModServerChannel();
    	$modServerChannel->deleteAll('channel_id=:channel_id', array(':channel_id'=>$channel_id));
    	return true;
    }
    
    public static function getServer($os,$g){
    	$modChannel = new ModChannel();
    	$server_list = $modChannel->getServer($os,$g);
    	return $server_list;
    }
    
    public static function getChannelForSpecial($special_id){
    	$modChannel = new ModChannel();
    	$list = $modChannel->getChannelForSpecial($special_id);
    	return $list;
    }
    
    public static function getChannel($params){
    	$modChannel = new ModChannel();
    	$list = $modChannel->select($params);
    	$data = array();
    	foreach ($list as $key=>$val){
    		$data[$val['id']] = $val['channel_name'];
    	}
    	return $data;
    }
    
    public static function status(){
    	$list = array(
    			'0'	 => '否',
    			'1'  => '是',
    	);
    	return $list;
    }
    
    /*
     * 去重
     */
    public static function crepeat($id = 0, $field, $val,$g){
    	$modChannel = new ModChannel();
    	$conut = $modChannel->crepeat($id, $field, $val,$g);
    	return $conut;
    }


}